/**
 *  列表页
 *
 * 作者 admin
 * 日期 2024-08-07 16:43:01
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-accidentReportSurveyAudit'),
        $query = $view.find('#query'),
        $accidentType = $view.find('#accidentType'),
        $accidentLevel = $view.find('#accidentLevel'),
        $searchForm = $view.find('#accidentReportSurvey-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        tableIns;

    form.verify(validate);
    form.render();
    initDictItemSelect_reportSurveyAudit();
    initTable();
    initDate();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            type: 'datetime',
            range: ['#start-date', '#end-date'],
        })
    }

    function initDictItemSelect_reportSurveyAudit(){
        //事故类型
        febs.initDictItemSelect($accidentType,'SAFETY_ACCIDENT_TYPE','',null);
        //事故等级
        febs.initDictItemSelect($accidentLevel,'SAFETY_ACCIDENT_LEVEL','',null);
    }
    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'accidentReportSurveyAuditTable',
            skin: '',
            height: getTableHeight(),
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'tenantName', title: '公司名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'accidentNo',align:'center', title: '事故编号',event: 'detail', width: 200,templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.accidentNo+'</p>'
                    }
                },
                {field: 'accidentTitle',align:'left', title: '事故名称', minWidth: 380},
                {field: 'accidentDate',align:'center', title: '发生时间', width: 180},
                {field: 'accidentPlace',align:'left', title: '发生地点', minWidth: 280},
                {field: 'accidentType',align:'center', title: '事故类型', width: 120,templet:function (d){
                    if(d.accidentType){
                            return febs.getDictLabel(accidentTypeList,d.accidentType)
                        }
                    }
                },
                {field: 'accidentLevel',align:'center', title: '事故等级', width: 220,templet:function (d){
                        if(d.accidentLevel){
                            return febs.getDictLabel(accidentLevelList,d.accidentLevel)
                        }
                    }
                },
                {field: 'obligationDeptName',align:'center', title: '责任部门', minWidth: 150},
                {field: 'obligationRealName',align:'center', title: '责任人', width: 120},
                {field: 'surveyStatus',align:'center', title: '调查状态', width: 140, templet: function (d) {
                        var flg = '';
                        switch (d.surveyStatus) {
                            case "1": {
                                flg = '<span class="layui-badge febs-bg-cyan">待指派</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span class="layui-badge febs-bg-orange">待调查</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span class="layui-badge febs-bg-volcano">进行中</span>'
                                break;
                            }
                            case "4": {
                                flg = '<span class="layui-badge febs-bg-blue">待审核</span>'
                                break;
                            }
                            case "5": {
                                flg = '<span class="layui-badge febs-bg-red">审核退回</span>'
                                break;
                            }
                            case "6": {
                                flg = '<span class="layui-badge febs-bg-green">完成</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                // {field: 'noticeDeptFlg',align:'center', title: '是否上报主管部门', width: 150, templet: function (d) {
                //         var flg = '';
                //         if (d.noticeDeptFlg == '1') {
                //             flg = '<span class="layui-badge febs-bg-cyan">已上报</span>'
                //         } else if (d.noticeDeptFlg == '0') {
                //             flg = '<span class="layui-badge febs-bg-red">未上报</span>'
                //         }
                //         return flg;
                //     }
                // },
                // {field: 'noticeDeptDate',align:'center', title: '上报日期', width: 120},
                {field: 'surveyDeadline',align:'center', title: '调查截止时间', width: 120},
                {title: '操作',align:'center', fixed:'right', toolbar: '#accidentReportSurvey-option', width: 120}
            ]]
        });
        tableIns.reload({url: ctx + 'accidentReport/list', where: getQueryParams(), page: {curr: 1}});
    }

    //操作列点击事件
    table.on('tool(accidentReportSurveyAuditTable)', function(obj){
        var data = obj.data; // 获取当前行数据
        var reportId = data.reportId;
        if(obj.event === 'detail'){
            febs.modal.open('事故上报详情', 'accidentReport/detail/'+reportId, {
                btn: ['关闭'],
                area: $(window).width() <= 750 ?  ['85%', '80%']: ['95%', '95%'],
                shadeClose:true,
                yes: function (index, layero) {
                    layer.closeAll();
                },
            });
        }else if (obj.event === 'survey-audit') {
            febs.modal.open('调查结果审核', 'accidentReport/surveyAudit/'+reportId, {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['85%', '80%']: ['95%', '95%'],
                btn2: function () {
                    layer.closeAll();
                },
                yes: function (index, layero) {
                    $('#reportSurveyAudit-edit').find('#submit').trigger('click');
                },
                data: {
                    reportId: data.reportId,
                    tenantId: data.tenantId,
                }

            });
        }
    });

    form.on('select(leaderDeptSelect)', function(obj){
        // 获取选中项 value
        var data={
            deptId : obj.value
        }
        $("#leaderUserSelect").empty();
        febs.getSync(ctx + 'user/deptUserList', data, function (r) {
            var userList = r.data;
            var optionDefault = '<option value=""></option>';
            var options = '';
            $.each(userList,function (index,item) {
                var option =  '<option value="'+item.userId+'">'+item.uname+'</option>';
                options += option;
            })
            $("#leaderUserSelect").append(optionDefault+options);
        });
        form.render();
    });

    table.on('sort(accidentReportSurveyAuditTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    function getQueryParams() {
        return {
            businessType:'surveyAudit',
            accidentNo: $searchForm.find('input[name="accidentNo"]').val()?.trim(),
            accidentTitle: $searchForm.find('input[name="accidentTitle"]').val()?.trim(),
            accidentType: $searchForm.find("select[name='accidentType']").find("option:selected").val(),
            accidentLevel: $searchForm.find("select[name='accidentLevel']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            tenantId: $searchForm.find("select[name='tenantSelect']").find("option:selected").val(),
            // surveyStatus: $searchForm.find("select[name='surveyStatus']").find("option:selected").val(),
            obligationDeptId: $searchForm.find("select[name='obligationDeptId']").find("option:selected").val(),
            obligationUserId: $searchForm.find("select[name='obligationUserId']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }
    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});